package com.sjzs.web.interceptor;

import java.io.IOException;

import net.sf.json.JSONException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor;
import com.sjzs.web.common.RemoteAccessAction;
import com.sjzs.web.vo.ResultVO;

public class ExceptionHandlerInterceptor extends AnnotationWorkflowInterceptor {
	private static final long serialVersionUID = -1317987168534467063L;
	private static final Logger LOGGER = Logger.getLogger(ExceptionHandlerInterceptor.class);

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		if (actionInvocation.getAction() instanceof RemoteAccessAction) {
			RemoteAccessAction action = (RemoteAccessAction) actionInvocation.getAction();
			ResultVO result = new ResultVO();
			result.setResultCode("-1");
			try {
				return super.intercept(actionInvocation);
			} catch (JSONException e) {
				LOGGER.error("it is possible to be a parameter errror", e);
				result.setResultMsg("it is possible to be a parameter errror");
				action.setResult(result);
			} catch (NoSuchBeanDefinitionException e) {
				LOGGER.error("service called is not found", e);
				result.setResultMsg("service called is not found");
				action.setResult(result);
			} catch (IOException e) {
				LOGGER.error(e.getMessage(), e);
				result.setResultMsg(e.getMessage());
				action.setResult(result);
			} catch (Exception e) {
				LOGGER.error("unknown exception", e);
				result.setResultMsg("unknown exception");
				action.setResult(result);
			}
			return ActionSupport.SUCCESS;
		} else {
			try {
				return super.intercept(actionInvocation);
			} catch (Exception e) {
				LOGGER.error("Exception", e);
				return ActionSupport.ERROR;
			}
		}
	}
}
